/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/binary-tree-zigzag-level-order-traversal
   @Language: C++
   @Datetime: 19-07-15 11:29
   */

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
	vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
		vector<vector<int> > levels;
		if(root==NULL) return levels;
		queue<TreeNode*> q;
		bool left2right=true;
		for(q.push(root); q.size(); left2right=!left2right){
			vector<int> level;
			for(int i=q.size(); i--; q.pop()){
				const auto r=q.front();
				if(left2right) level.push_back(r->val);
				else level.insert(level.begin(),r->val);
				if(r->left) q.push(r->left);
				if(r->right) q.push(r->right);
			}
			levels.push_back(level);
		}
		return levels;
	}
};
